#coding=utf-8
import env
import wx.lib.pubsub as ps
import gui_campaign as gcamp
import gui_ad as gad
import wx

def OnCampSelected(msg):
    c = msg.data
    print 'Camp.Selected:%s'%c.name
    
    env.current_camp = c
    m = env.mainFrame
    m.SetListCtrl_byCamp(c)
    
    if not m.bp:
        m.bp = gcamp.CampPanel(m)
        m.bp.SetContent(c)
        m.mgr.AddPane(m.bp,
                  wx.aui.AuiPaneInfo().
                  Bottom().BestSize((400, -1)).
                  MinSize((300, -1)).
                  CloseButton(False).
                  Name("Camp"))
    else:
        m.bp.SetContent(c)
        
    m.mgr.GetPane("Ad").Hide()
    m.mgr.GetPane("Camp").Show()
    m.mgr.Update()
                             
ps.Publisher.subscribe(OnCampSelected, 'Camp.Selected')

def OnCampChangeApplied(msg):
    c = msg.data
    print 'Camp.ChangeApplied:%s'%c.name

    env.current_camp = c
    m = env.mainFrame
    m.SetListCtrl_byCamp(c)
    m.mgr.GetPane("Ad").Hide()
    m.mgr.GetPane("Camp").Hide()
    m.mgr.Update()
ps.Publisher.subscribe(OnCampChangeApplied, 'Camp.ChangeApplied')  

def OnCampAdded(msg):
    c = msg.data
    print 'Camp.Added:%s'%c.name

    env.current_camp = c
    
    m = env.mainFrame
    
    items = env.generate_camp_tree()
    print items
    m.tree.DeleteAllItems()
    root = m.tree.AddRoot("Campaigns")
    m.AddTreeNodes(root, items)
    m.tree.SelectItem(m.tree.GetFirstChild(m.tree.GetRootItem())[0])
    
    m.SetListCtrl_byCamp(c)
    m.mgr.GetPane("Ad").Hide()
    m.mgr.GetPane("Camp").Hide()
    m.mgr.Update()
ps.Publisher.subscribe(OnCampAdded, 'Camp.Added')

def OnAdSelected(msg):
    a = msg.data
    print 'Ad.Selected:%s'%a.name
    
    env.current_ad = a
    m = env.mainFrame
    
    if not m.ap:
        m.ap = gad.AdPanel(m)
        m.ap.SetContent(a)
        m.mgr.AddPane(m.ap,
                  wx.aui.AuiPaneInfo().
                  Bottom().BestSize((400, -1)).
                  MinSize((300, -1)).
                  CloseButton(False).
                  Name("Ad"))
    else:
        m.bp.SetContent(a)
        
    m.mgr.GetPane("Camp").Hide()
    m.mgr.GetPane("Ad").Show()
    m.mgr.Update()
ps.Publisher.subscribe(OnAdSelected, 'Ad.Selected')

def OnAdChangeApplied(msg):
    a = msg.data
    print 'Ad.AdChangeApplied:%s'%a.name
    
    env.current_ad = a
    m = env.mainFrame
    
    m.mgr.GetPane("Ad").Hide()
    m.mgr.GetPane("Camp").Hide()
    m.mgr.Update()

ps.Publisher.subscribe(OnAdChangeApplied, 'Ad.ChangeApplied')  

    
